package com.lfs.admin.controller.channel;

import com.lfs.admin.model.entity.channel.ChannelInfoEntity;
import com.lfs.admin.model.entity.channel.ChannelToGroupEntity;
import com.lfs.admin.model.entity.channel.ChannelToProductEntity;
import com.lfs.admin.model.vo.channel.ChannelGroupVO;
import com.lfs.admin.model.vo.channel.ChannelToGroupVO;
import com.lfs.admin.model.vo.channel.ChannelToProductVO;
import com.lfs.admin.model.vo.channel.ChannelVO;
import com.lfs.admin.service.ChannelInfoService;
import com.lfs.base.dto.Result;
import com.lfs.base.dto.ResultObject;
import com.lfs.base.exception.BusinessException;
import com.lfs.base.util.CollectionUtils;
import com.lfs.base.util.StringUtils;
import com.lfs.common.annotation.Log;
import com.lfs.common.core.controller.BaseController;
import com.lfs.common.core.domain.AjaxResult;
import com.lfs.common.core.page.TableDataInfo;
import com.lfs.common.enums.BusinessType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/channel")
public class ChannelInfoController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(ChannelInfoController.class);

    @Autowired
    private ChannelInfoService channelInfoService;

    /**
     * 查询上游下拉框接口
     * @param channelInfoVO
     * @return
     * @throws Exception
     */
    @PreAuthorize("@ss.hasPermi('channel:channelInfo:query')")
    @PostMapping("/querySelectList")
    public Result<?> querySelectList(@RequestBody ChannelVO channelInfoVO) {
        logger.info("根据{},查询上游供应商列表请求参数", channelInfoVO.toString());
        List<ChannelInfoEntity> channelList = channelInfoService.querySelectList(channelInfoVO);
        return ResultObject.successObject(channelList, "查询列表成功");
    }

    /**
     * 查询上游列表接口
     * @param channelVO
     * @return
     * @throws Exception
     */
    @PreAuthorize("@ss.hasPermi('channel:channelInfo:query')")
    @PostMapping("/queryChannelInfoList")
    public TableDataInfo queryChannelInfoList(ChannelVO channelVO) {

        if(null == channelVO){
            throw new BusinessException("请求上游列表参数不能为空!");
        }

        logger.info("根据{},查询上游列表接口请求参数", channelVO.toString());
        startPage();
        List<ChannelInfoEntity> channelList = channelInfoService.queryChannelInfoList(channelVO);
        return getDataTable(channelList);
    }

    /**
     * 查询通道信息接口
     * @param id
     * @return
     */
    /**
     * 根据通道编码获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('channel:channelInfo:query')")
    @GetMapping(value = { "/getChannelInfo/{id}"})
    public AjaxResult getChannelInfo(@PathVariable(value = "id", required = false) Integer id)
    {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(id))
        {
            ajax.put(AjaxResult.DATA_TAG, channelInfoService.getChannelById(id));
        }
        return ajax;
    }

    /**
     * 查询所有通道下拉框列表
     */
    @PreAuthorize("@ss.hasPermi('channel:channelInfo:query')")
    @PostMapping(value = { "/getChannelList"})
    public AjaxResult getChannelList()
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put(AjaxResult.DATA_TAG, channelInfoService.getChannelList());
        return ajax;
    }

    /**
     * 查询所有通道下拉框列表
     */
    @PreAuthorize("@ss.hasPermi('channel:channelInfo:query')")
    @PostMapping(value = { "/getSelectChannel"})
    public AjaxResult getSelectChannel(ChannelGroupVO channelGroupVO)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put(AjaxResult.DATA_TAG, channelInfoService.getChannelToGroupList(channelGroupVO.getId()));
        return ajax;
    }

    /**
     * 查询通道下产品列表接口
     * @param channelToProductVO
     * @return
     * @throws Exception
     */
    @PreAuthorize("@ss.hasPermi('channel:channelInfo:query')")
    @PostMapping("/getChannelToProduct")
    public TableDataInfo getChannelToProduct(ChannelToProductVO channelToProductVO) {
        logger.info("根据{},查询通道下产品列表请求参数", channelToProductVO.toString());
        startPage();
            List<ChannelToProductEntity> channelToGroupList = channelInfoService.getChannelToProduct(channelToProductVO);
        return getDataTable(channelToGroupList);
    }

    /**
     * 更新通道信息
     * @param channelVO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('channel:channelInfo:update')")
    @PutMapping("/updateChannelInfo")
    @Log(title = "更新通道信息", businessType = BusinessType.UPDATE)
    public Result<?> updateChannelInfo(@RequestBody ChannelVO channelVO){
        if(null == channelVO.getChannelNum()){
            throw new BusinessException("请求更新的通道编码不能为空!");
        }
        int result = channelInfoService.updateChannelInfo(channelVO);
        if(result <= 0){
            logger.info("未更新到任意一条记录!");
        }
        return  ResultObject.successMessage("更新成功!");
    }

    /**
     * 通道状态修改
     */
    @PreAuthorize("@ss.hasPermi('channel:channelInfo:update')")
    @Log(title = "通道状态修改", businessType = BusinessType.UPDATE)
    @PutMapping("/changeChannelStatus")
    public AjaxResult changeChannelStatus(@RequestBody ChannelVO channelVO)
    {
        return toAjax(channelInfoService.updateChannelStatus(channelVO));
    }

    /**
     * 通道组下通道产品状态修改
     */
    @PreAuthorize("@ss.hasPermi('channel:channelInfo:update')")
    @Log(title = "通道组下通道产品修改", businessType = BusinessType.UPDATE)
    @PutMapping("/updateChannelProductState")
    public AjaxResult updateChannelProductState(@RequestBody ChannelToProductVO channelToProductVO)
    {
        return toAjax(channelInfoService.updateChannelProductState(channelToProductVO));
    }

    /**
     * 新增通道信息
     * @param channelVO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('channel:channelInfo:add')")
    @PostMapping("/addChannelInfo")
    @Log(title = "新增通道信息", businessType = BusinessType.INSERT)
    public Result<?> addChannelInfo(@RequestBody ChannelVO channelVO){
        if(null == channelVO.getChannelNum()){
            throw new BusinessException("通道编码不能为空!");
        }
        if(null == channelVO.getChannelName()){
            throw new BusinessException("通道名称不能为空!");
        }
        if(null == channelVO.getChannelSt()){
            throw new BusinessException("通道状态不能为空!");
        }
        if(null == channelVO.getMakeOrderUrl()){
            throw new BusinessException("下单地址不能为空!");
        }
        if(null == channelVO.getAdapterName()){
            throw new BusinessException("通道适配器地址不能为空!");
        }
        int result = channelInfoService.addChannelInfo(channelVO);
        if(result <= 0){
            throw new BusinessException("新增通道失败!");
        }
        return  ResultObject.successMessage("新增通道成功!");
    }

    /**
     * 通道新增产品信息
     * @param channelToProductVO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('channel:channelInfo:add')")
    @PostMapping("/addChannelToProduct")
    @Log(title = "通道添加产品管理", businessType = BusinessType.INSERT)
    public Result<?> addChannelToProduct(@RequestBody ChannelToProductVO channelToProductVO){
        if(null == channelToProductVO.getChannelNum()){
            throw new BusinessException("通道编码不能为空!");
        }
        if(CollectionUtils.isEmpty(channelToProductVO.getProductNums())){
            throw new BusinessException("产品编码不能为空!");
        }
        int result = channelInfoService.addChannelToProduct(channelToProductVO);
        if(result <= 0){
            throw new BusinessException("通道新增产品失败!");
        }
        return  ResultObject.successMessage("通道新增产品成功!");
    }

    /**
     * 删除通道信息
     * @param ChannelIds
     * @return
     */
    @PreAuthorize("@ss.hasPermi('channel:channelInfo:remove')")
    @Log(title = "删除通道", businessType = BusinessType.DELETE)
    @DeleteMapping("/delChannelInfo/{ChannelIds}")
    public AjaxResult delChannelInfo(@PathVariable Integer[] ChannelIds){
        return toAjax(channelInfoService.deleteChannelInfo(ChannelIds));
    }

}
